package br.estacio.contatos.client.activity;

import java.util.List;

import br.estacio.contatos.client.place.ListarContatoPlace;
import br.estacio.contatos.client.service.ContatoRemoteServiceAsync;
import br.estacio.contatos.client.ui.ListarContatoView;
import br.estacio.contatos.shared.Contato;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ListarContatoActivity extends AbstractActivity {
	
	private final ListarContatoView listarContatoView;
	private final ContatoRemoteServiceAsync contatoRemoteServiceAsync;

	public ListarContatoActivity(ListarContatoPlace place, ListarContatoView view, ContatoRemoteServiceAsync serviceAsync) {
		this.listarContatoView = view;
		this.contatoRemoteServiceAsync = serviceAsync;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		panel.setWidget(listarContatoView);
		contatoRemoteServiceAsync.listarTodos(new AsyncCallback<List<Contato>>() {
			@Override
			public void onSuccess(List<Contato> result) {
				listarContatoView.setListagemContatos(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				// TODO notificar falha na listagem
			}
		});
	}

}
